package leetcode.map;

import java.util.ArrayDeque;
import java.util.List;

public class Lc841 {

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] visited = new boolean[rooms.size()];
        ArrayDeque<Integer> queue = new ArrayDeque<>();
        queue.offerLast(0);
        visited[0] = true;
        int cnt = 1;
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                int room = queue.pollFirst();
                for (int idx : rooms.get(room)) {
                    if (!visited[idx]) {
                        cnt++;
                        if (cnt == rooms.size()) return true;
                        queue.offerLast(idx);
                        visited[idx] = true;
                    }
                }
            }
        }
        return false;
    }

}
